import store from '@/store'
import router, {resetRouter} from '@/router'

function pushRoute(array) {
    let result = []
    array.forEach(item => {
        if (item.children.length > 0) {
            item.children.forEach(child => {
                result.push(add(child))
            })
        } else {
            result.push(add(item))
        }
    })
    return result
}

function add(obj) {
    return  {
        path: obj.path,
        name: obj.path.replace('/', ''),
        meta: {
            title: obj.name,
        },
        component: () => import('@/' + obj.component),
        children: []
    }
}



export const permissionRouter = function() {
    let menus = store.getters.asyncRoutes
    const asyncRoute = {
        path: '/layout',
        name: 'layout',
        meta: {
            title: '课程社区系统'
        },
        component: () => import('@/views/layout/index'),
        children: pushRoute(menus)
    }
    router.addRoute(asyncRoute)
}
